package com.java1234.web;

import java.io.IOException;
import java.io.OutputStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.java1234.dao.MessageDao;
import com.java1234.model.Message;
import com.java1234.util.DbUtil;
import com.java1234.util.ExportExcelUtils;

public class ExportExcel02Servlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
	 DbUtil dbUtil=new DbUtil();
	  MessageDao messageDao=new MessageDao();
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	     // File file = new File(getServletContext().getRealPath("WEB-INF/book.jpg"));
		  
	      response.setContentType("octets/stream");
	      response.addHeader("Content-Disposition", "attachment;filename=import.xls");						
		 String ids = request.getParameter("ids");
		 Connection con= null;
			try {
				con = dbUtil.getCon();
			} catch (Exception e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			
			List<Message> exportMessage = null;
			try {
				exportMessage= messageDao.exportMessage(con, ids);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		 //System.out.println(ids);
			// 测试信息
		      ExportExcelUtils<Message> ex = new ExportExcelUtils<Message>();
		      String[] headers = { "年份", "分类", "选题方向", "项目名称", "项目形式", "项目负责人", "职称", "工作单位", "联系方式","项目组成员", "备注", "是否立项","是否结项","获奖情况" };
		      List<Message> dataset = new ArrayList<Message>();
		      for(int i = 0;i<exportMessage.size(); i++){
		    	  dataset.add(exportMessage.get(i));
		      }
		      Collection datesetCo=dataset;
		      
		      OutputStream out = response.getOutputStream();
		         ex.exportExcel(headers, datesetCo, out);
		         out.close();
		         System.out.println("excel导出成功！");
	      //dataset.add(new Student(30000003, "王五", 22, true, new Date()));
	     /* //测试图书
	      ExportExcelUtils<Book> ex = new ExportExcelUtils<Book>();
	      String[] headers = { "图书编号", "图书名称", "图书作者", "图书价格", "图书ISBN",
	            "图书出版社", "封面图片" };
	      List<Book> dataset = new ArrayList<Book>();
	      try {
	         BufferedInputStream bis = new BufferedInputStream(
	               new FileInputStream(file));
	         byte[] buf = new byte[bis.available()];
	         while ((bis.read(buf)) != -1) {
	            //将图片数据存放到缓冲数组中
	         }
	         dataset.add(new Book(1, "jsp", "leno", 300.33f, "1234567",
	                "清华出版社", buf));
	         dataset.add(new Book(2, "java编程思想", "brucl", 300.33f, "1234567",
	                "阳光出版社", buf));
	         dataset.add(new Book(3, "DOM艺术", "lenotang", 300.33f, "1234567",
	                "清华出版社", buf));
	         dataset.add(new Book(4, "c++经典", "leno", 400.33f, "1234567",
	                "清华出版社", buf));
	         dataset.add(new Book(5, "c#入门", "leno", 300.33f, "1234567",
	                "汤春秀出版社", buf));*/
	         //OutputStream out = new FileOutputStream("E://a.xls");
	         //OutputStream out = new FileOutputStream("E://b.xls");
	         
	         //ex.exportExcel(headers, dataset, out);
	         //out.close();
	         //JOptionPane.showMessageDialog(null, "导出成功!");
	         //System.out.println("excel导出成功！");
	         
	         
	         /*OutputStream out = response.getOutputStream();
	         ex.exportExcel(headers, datesetCo, out);
	         //out.close();
	         System.out.println("excel导出成功！")*/;
	     /* } catch (FileNotFoundException e) {
	         // TODO Auto-generated catch block
	         e.printStackTrace();
	      } catch (IOException e) {
	         // TODO Auto-generated catch block
	         e.printStackTrace();
	      }*/;
	}


	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
